﻿using System;
using System.Text;

namespace GameLogger
{
    ///// <summary>
    ///// 日志等级
    ///// </summary>
    //public enum ELevel
    //{
    //    /// <summary>
    //    /// 调试日志：开发过程中的调试信息。正式发布版本，应该默认关闭。
    //    /// </summary>
    //    Debug,

    //    /// <summary>
    //    /// 运行日志：程序运转过程中的关键日志。默认开启。
    //    /// </summary>
    //    Info,

    //    /// <summary>
    //    /// 警告日志：默认开启。
    //    /// </summary>
    //    Warn,

    //    /// <summary>
    //    /// 错误日志：无法关闭。
    //    /// </summary>
    //    Error,

    //    /// <summary>
    //    /// 异常日志：无法关闭。
    //    /// </summary>
    //    Exception,
    //}

    /// <summary>
    /// ILogger
    /// </summary>
    public interface ILogger
    {
        /// <summary>
        /// 设置是否开启debug等级日志
        /// </summary>
        /// <param name="on"></param>
        void EnableDebug(bool on);

        /// <summary>
        /// 设置是否开启info等级日志
        /// </summary>
        /// <param name="on"></param>
        void EnableInfo(bool on);

        /// <summary>
        /// 记录debug等级日志
        /// </summary>
        /// <param name="message"></param>
        void Debug(object message);

        /// <summary>
        /// 记录infomation等级日志
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        void Debug(string format, params object[] args);

        /// <summary>
        /// 记录infomation等级日志
        /// </summary>
        /// <param name="message"></param>
        void Info(object message);

        /// <summary>
        /// 记录warning等级日志
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        void Info(string format, params object[] args);

        /// <summary>
        /// 记录warning等级日志
        /// </summary>
        /// <param name="message"></param>
        void Warn(object message);

        /// <summary>
        /// 记录debug等级日志
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        void Warn(string format, params object[] args);

        /// <summary>
        /// 记录error等级日志
        /// </summary>
        /// <param name="message"></param>
        void Error(object message);

        /// <summary>
        /// 记录error等级日志
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        void Error(string format, params object[] args);

        /// <summary>
        /// 记录exception
        /// </summary>
        /// <param name="exception"></param>
        void Exception(Exception exception);
    }


    /// <summary>
    /// 日志记录器
    /// </summary>
    class LoggerInner
    {
        /// <summary>
        /// 名称
        /// </summary>
        private readonly string mName;

        /// <summary>
        /// 追加输出名称
        /// </summary>
        private readonly bool mOutputModuleName;

        /// <summary>
        /// 追加输出时间
        /// </summary>
        private readonly bool mOutputTime;

        /// <summary>
        /// 是否开启debug等级日志
        /// </summary>
        protected bool mEnableDebug;

        /// <summary>
        /// 是否开启infomation等级日志
        /// </summary>
        protected bool mEnableInfo;

        /// <summary>
        /// Logger
        /// </summary>
        /// <param name="moduleName"></param>
        /// <param name="enableDebug"></param>
        /// <param name="enableInfo"></param>
        /// <param name="outputModuleName"></param>
        /// <param name="outputTime"></param>
        protected LoggerInner(string moduleName, bool enableDebug, bool enableInfo,
            bool outputModuleName, bool outputTime)
        {
            mName = moduleName;
            mEnableDebug = enableDebug;
            mEnableInfo= enableInfo;
            mOutputModuleName = outputModuleName;
            mOutputTime = outputTime;

            mSB = new StringBuilder(1024);
        }

        private StringBuilder mSB;

        /// <summary>
        /// 根据设置，格式化给定的字符串
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected object Format(object message)
        {
            mSB.Remove(0, mSB.Length);

            bool changed = false;
            if (mOutputModuleName)
            {
                changed = true;
                mSB.Append(mName);
                mSB.Append(' ');
            }

            if (mOutputTime)
            {
                changed = true;
                mSB.Append(DateTime.Now.ToString("HH:mm:ss"));
                mSB.Append(' ');
            }

            if (changed)
            {
                mSB.Append(message);
                return mSB.ToString();
            }

            return message;
        }

        /// <summary>
        /// 根据设置，格式化给定的字符串
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        protected object Format(string format, params object[] args)
        {
            mSB.Remove(0, mSB.Length);

            if (mOutputModuleName)
            {
                mSB.Append(mName);
                mSB.Append(' ');
            }

            if (mOutputTime)
            {
                mSB.Append(DateTime.Now.ToString("HH:mm:ss"));
                mSB.Append(' ');
            }

            mSB.AppendFormat(format, args);

            return mSB.ToString();
        }
    }
}
